package com.ash.dao;

import com.ash.domain.Student;
import com.ash.util.ConnectionsUtil;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @encoding : UTF-8
 * @Time : 2020/12/8 18:16
 * @Author : Chen Chao
 * @File : connection.com.ash.dao.BaseDao.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class BaseDao<T> {
    private Class<T> inClass;

    public BaseDao() {
    }

    public BaseDao(Class<T> inClass) {
        this.inClass = inClass;
    }

    /**
     * sqlUpdate执行方法模板
     * @param sql  sql模板
     * @param objects 类型不确定所以用Object
     */
    public static void executeUpdate(String sql,ArrayList<Object> objects ){
        /*建立连接*/
        Connection connection = ConnectionsUtil.getConnection();
        PreparedStatement ps = null;
        /*写入数据*/
        try {
            ps = Objects.requireNonNull(connection).prepareStatement(sql);
            for (int i = 0; i < objects.size(); i++) {
                ps.setObject(i+1,objects.get(i));
            }
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            /*释放资源*/
            ConnectionsUtil.close(connection, ps, null);
        }
    }

    /**
     * sqlQuery执行方法模板
     * @param sql sql模板
     * @param objects 类型不确定所以用Object
     * @return 查询结果集
     */
    public  List<T> executeQuery(String sql, ArrayList<Object> objects){
        /*建立连接*/
        Connection connection = ConnectionsUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        /*创建类对象*/
        ArrayList<T> ts = new ArrayList<>();
        try {
            ps = Objects.requireNonNull(connection).prepareStatement(sql);
            if(objects!=null){
                for (int i = 0; i < objects.size(); i++) {
                    ps.setObject(i+1,objects.get(i));
                }
            }
            resultSet = ps.executeQuery();

            /*读取数据*/
            BeanInfo beanInfo = Introspector.getBeanInfo(inClass, Object.class);

            PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

            while (resultSet.next()) {
                T t = inClass.newInstance();
                for (PropertyDescriptor pd : pds) {
                    Method writeMethod = pd.getWriteMethod();
                    writeMethod.invoke(t, resultSet.getObject(pd.getName()));
                }
                ts.add(t);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        } catch (IllegalAccessException | InstantiationException | IntrospectionException | InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            /*释放资源*/
            ConnectionsUtil.close(connection, ps, resultSet);
        }
        return ts;
    }

    public Class<T> getInClass() {
        return inClass;
    }

    public void setInClass(Class<T> inClass) {
        this.inClass = inClass;
    }
}
